package cn.zd.erp.auth.user.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.zd.erp.auth.role.service.ebi.RoleEbi;
import cn.zd.erp.auth.role.vo.Trole;
import cn.zd.erp.auth.user.service.ebi.UserEbi;
import cn.zd.erp.auth.user.vo.Tuser;
import cn.zd.erp.util.base.BaseAction;
import cn.zd.erp.util.base.Grid;
import cn.zd.erp.util.base.Json;
import cn.zd.erp.util.base.PageFilter;
import cn.zd.erp.util.base.PageResults;
import cn.zd.erp.util.base.User;

@Controller
@Scope(value = "prototype")
public class UserAction extends BaseAction {
	private PageResults<User> pageResults;
	private String roleIds;
	private Tuser user;
	private Json json = new Json();
	@Autowired
	private UserEbi userEbi;
	@Autowired
	private RoleEbi roleEbi;
	private Grid grid;

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public Json getJson() {
		return json;
	}

	public Grid getGrid() {
		return grid;
	}

	// 返回用户全部分页数据
	public String dataGrid() {
		grid = new Grid();
		pageResults = userEbi.dataGridCount(user, page, sort, rows, order);
		grid.setRows(pageResults.getResults());
		System.out.println(pageResults.getTotalCount() + "修改过以后的");
		grid.setTotal((long) pageResults.getTotalCount());
		return "datajson";
	}

	// 新增用户
	public String sava() {
		user.setCreatedatetime(new Date());
		user.setIsdefault(0);
		user.setState(0);
		Tuser u = userEbi.getByName(user.getLoginname());
		if (u != null) {
			json.setMsg("用户名已存在!");
		} else {
			try {
				List<Trole> roles = new ArrayList<Trole>();
				if (roleIds != null) {
					for (String roleId : roleIds.split(", ")) {
						roles.add(roleEbi.get(Long.valueOf(roleId)));
					}
				}
				user.setRoles(new HashSet<Trole>(roles));
				userEbi.save(user);
				json.setSuccess(true);
				json.setMsg("添加成功！");
			} catch (Exception e) {
				json.setMsg(e.getMessage());
			}

		}
		return "resultjson";
	}

	// 页面跳转
	// 编辑用户
	public String page() {
		if (user != null) {
			user = userEbi.get(user.getId());
			roleIds = userEbi.getChRole(user);
		}
		return SUCCESS;
	}

	// 修改保存
	public String edit() {
		try {
			List<Trole> roles = new ArrayList<Trole>();
			if (roleIds != null) {
				for (String roleId : roleIds.split(", ")) {
					System.out.println(Long.valueOf(roleId));
					roles.add(roleEbi.get(Long.valueOf(roleId)));
				}
			}
			user.setRoles(new HashSet<Trole>(roles));
			userEbi.save(user);
			json.setSuccess(true);
			json.setMsg("修改成功！");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return "resultjson";
	}

	public String delete() {
		try {
			userEbi.delete(user);
			json.setSuccess(true);
			json.setMsg("删除成功！");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return "resultjson";
	}

	public Tuser getUser() {
		return user;
	}

	public void setUser(Tuser user) {
		this.user = user;
	}

}
